package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.activity.toJSONString
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.Condition7
import com.abc.robot01.retrofit.bean.FetchDataRequest9
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch


class CustomerDetailViewModel : ViewModel() {
    val TAG = "CustomerDetailViewModel 客户渲染记录:"
    val customerName = mutableStateOf("")
    val totalAmount = mutableStateOf("15.08")
    val count = mutableStateOf("0")
    val customerPhone = mutableStateOf("")

    //客户列表
    private val _customerList = MutableStateFlow<List<RetrofitService.Task>>(emptyList())
    val customerList: StateFlow<List<RetrofitService.Task>> = _customerList

    fun load() {
        Log.e(TAG, "客户渲染记录列表:${customerName.value}")
        Log.e(TAG, "客户渲染记录列表:${customerPhone.value}")
        Log.e(TAG, "客户渲染记录列表: Robot.companyId: ${Robot.companyId}")
        viewModelScope.launch {
            val res = RetrofitService.instance.getCustomerDetails(
                accessToken = Robot.accessToken,
                userId = Robot.userId,
                companyId = Robot.companyId,
                request = FetchDataRequest9(
                    condition = Condition7(
                        customerName = customerName.value,
                        customerPhone = customerPhone.value
                    )
                )
            )
            Log.e(TAG, "客户渲染记录列表:$res")
            if (res.code == 200) {
                _customerList.value = res.data.list
                count.value = res.data.list.size.toString()
            }
        }
    }
}
